package com.shop.config;

import cn.hutool.core.util.ObjectUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class OpenFieldInterConfig implements RequestInterceptor {


    @Override
    public void apply(RequestTemplate requestTemplate) {
        //1.拿到请求
        //2.判断请求
        //3.设置token传递
        //4.设置永久
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        if (!ObjectUtil.isEmpty(request)) {
            String authorization = request.getHeader("Authorization");
            if (!StringUtils.isEmpty(authorization)) {
                requestTemplate.header("Authorization", authorization);
            } else {
                //永久
                requestTemplate.header("Authorization", "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJyZWFkIl0sImV4cCI6Mzc2MTU1NjcwNiwianRpIjoiNjc3YTA0YzMtMDZhMC00YTZkLTlmZDgtNDkzMzVmN2MwNDY4IiwiY2xpZW50X2lkIjoiY2xpZW50In0.Kqa83oWTtIfnBFFjWueDX444MA2l9PQfRyNDfFmfkqXIftUwZCHQRVq4hJkYR2l8o5H8i1FkjCJzO6gkf8WtedtOBtyeqaV_HNcuwA4hkvgh59TcicKWs6ar_fFlzOlVZ8uGRNM-CWZpcMchYfeHLDfyVBEgFv3CMZHZ82gUcVwyi5vSHre20QKo6CVgRqTwvLKfchSmJd46fz49vJBG9mY9xb6FMnjwDRaCXUXhGVLbsxMeRTW5yUsa7UdLYnkpK8Nlk8nNYKN_O1KH7u_1-9QJVCI5N8qZGGb-51KaPtjjtBJ-NitMyGVboS5wCd5sCtyPOlkHUdyNvW_CAQWP4g");
            }
        } else {
            //永久
            requestTemplate.header("Authorization", "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJyZWFkIl0sImV4cCI6Mzc2MTU1NjcwNiwianRpIjoiNjc3YTA0YzMtMDZhMC00YTZkLTlmZDgtNDkzMzVmN2MwNDY4IiwiY2xpZW50X2lkIjoiY2xpZW50In0.Kqa83oWTtIfnBFFjWueDX444MA2l9PQfRyNDfFmfkqXIftUwZCHQRVq4hJkYR2l8o5H8i1FkjCJzO6gkf8WtedtOBtyeqaV_HNcuwA4hkvgh59TcicKWs6ar_fFlzOlVZ8uGRNM-CWZpcMchYfeHLDfyVBEgFv3CMZHZ82gUcVwyi5vSHre20QKo6CVgRqTwvLKfchSmJd46fz49vJBG9mY9xb6FMnjwDRaCXUXhGVLbsxMeRTW5yUsa7UdLYnkpK8Nlk8nNYKN_O1KH7u_1-9QJVCI5N8qZGGb-51KaPtjjtBJ-NitMyGVboS5wCd5sCtyPOlkHUdyNvW_CAQWP4g");

        }
    }
}
